﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using BTCe;
using BTCe.Data;
using BTCe.Constant;

namespace BTCe.Common
{
    public class OrderExchange
    {
        BTCeAccessInfo _AccessInfo;

        public OrderExchange()
        {
            _AccessInfo = null;
        }

        public OrderExchange(BTCeAccessInfo _info)
        {
            _AccessInfo = _info;
        }

        public string OrderBuy(string strBTCPrice, string strBTCVolume)
        {
            string strReturn = "";
            string strData = "";
            string strURL = "";
            string strParameters = "";


            //로그인 하지 않았다면
            if (_AccessInfo == null)
                return strReturn;
            if (!_AccessInfo.IsLogin())
                return strReturn;

            string strPair = "btc_usd";
            string strType = "buy";
            string strPrice = strBTCPrice;
            string strAmount = strBTCVolume;

            Dictionary<string, string> dicURLParameter = _AccessInfo.GetURLParameterAccessKey();
            dicURLParameter.Add("method", "Trade");
            dicURLParameter.Add("pair", strPair);
            dicURLParameter.Add("type", strType);
            dicURLParameter.Add("rate", strPrice);
            dicURLParameter.Add("amount", strAmount);


            //접속 주소를 입력한다.
            strURL = _AccessInfo.GetBaseURL() + BTCeURL.URL_ORDER_BUY;
            strParameters = CommonFunction.ConvertURLParameter(dicURLParameter);

            strData = Communication.GetRequestData(strURL, strParameters, _AccessInfo.GetHttpHeaderAPI(strParameters));

            if (strData.Length == 0)
                return strReturn;

            strReturn = strData;

            return strReturn;
        }

        public string OrderSell(string strBTCPrice, string strBTCVolume)
        {
            string strReturn = "";
            string strData = "";
            string strURL = "";
            string strParameters = "";


            //로그인 하지 않았다면
            if (_AccessInfo == null)
                return strReturn;
            if (!_AccessInfo.IsLogin())
                return strReturn;

            string strPair = "btc_usd";
            string strType = "sell";
            string strPrice = strBTCPrice;
            string strAmount = strBTCVolume;

            Dictionary<string, string> dicURLParameter = _AccessInfo.GetURLParameterAccessKey();
            dicURLParameter.Add("method", "Trade");
            dicURLParameter.Add("pair", strPair);
            dicURLParameter.Add("type", strType);
            dicURLParameter.Add("rate", strPrice);
            dicURLParameter.Add("amount", strAmount);


            //접속 주소를 입력한다.
            strURL = _AccessInfo.GetBaseURL() + BTCeURL.URL_ORDER_BUY;
            strParameters = CommonFunction.ConvertURLParameter(dicURLParameter);

            strData = Communication.GetRequestData(strURL, strParameters, _AccessInfo.GetHttpHeaderAPI(strParameters));

            if (strData.Length == 0)
                return strReturn;

            strReturn = strData;

            return strReturn;
        }

        public string OrderCancel(string strOrderID)
        {
            string strReturn = "";
            string strData = "";
            string strURL = "";
            string strParameters = "";


            //로그인 하지 않았다면
            if (_AccessInfo == null)
                return strReturn;
            if (!_AccessInfo.IsLogin())
                return strReturn;

            Dictionary<string, string> dicURLParameter = _AccessInfo.GetURLParameterAccessKey();
            dicURLParameter.Add("method", "CancelOrder");
            dicURLParameter.Add("order_id", strOrderID);


            //접속 주소를 입력한다.
            strURL = _AccessInfo.GetBaseURL() + BTCeURL.URL_ORDER_CANCEL;
            strParameters = CommonFunction.ConvertURLParameter(dicURLParameter);

            strData = Communication.GetRequestData(strURL, strParameters, _AccessInfo.GetHttpHeaderAPI(strParameters));

            if (strData.Length == 0)
                return strReturn;

            strReturn = strData;

            return strReturn;
        }

        public string OpenOrderList()
        {
            string strReturn = "";

            return strReturn;
        }
    }
}
